using Newtonsoft.Json.Serialization;
using RuoVea.ExFilter;
//using RuoVea.ExJwtBearer;
using RuoVea.ExSugar;
//using RuoVea.OmiTest.Server.SeedData;
using RuoVea.OmiTest.Web.Filters;
using RuoVea.OmiConfig;
using RuoVea.OmiConfig.Extensions;
using RuoVea.OmiLog;
//using RuoVea.OmiLogin;
//using RuoVea.OmiUserRoleMenu;
using System.Diagnostics;

namespace RuoVea.OmiTest.Web;
public class Program
{
    public static async Task Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.Services.AddLocalization();

        // Add services to the container.
        builder.Services.AddControllersWithViews(/*options => options.Filters.Add<GlobalAuthorizeFilter>()*/)
            .AddNewtonsoftJson(options =>
            {
                //��ʹ���շ�������ʽ
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                //����ʱ���ʽ
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            });

        // �������л�ȡ
        builder.Services.Configure<SystemInfo>(builder.Configuration.GetSection("SystemInfo"));

        builder.Services
            //.AddHttpContextSetup<AspNetUser>()
            .AddOmiConfigSetup()
            .AddOmiLogSetup()
            //.AddOmiSystemSetup()
            //.AddOmiLoginSetup(o =>
            //{
            //    o.DefaultUrl = "/Home";
            //    o.RedirectUrl = "/Home";
            //    o.LoginPath = "/auth/login";
            //    o.Scheme = "RuoVea.OmiTest:" + o.Scheme;
            //    o.ShowI18n = true;
            //})
            ;

        builder.Services
            .AddSqlSugarSetup();

        builder.Services
            .AddConfigInitSetup()
            //.AddAuthInitSetup()
            .AddLogInitSetup()
            //.AddSystemInitSetup()
            ;

        //builder.Services.AddDictInitSetup();/*��Ҫ�� AddSqlSugarSetup֮��*/


        builder.Services
            //.AddRestfulSetup()
            .RequestActionSetup() // ע�� ������־���� [ִ�в�����־��������֤ ]
            .ResultSetup()
            .ExceptionSetup();

        var app = builder.Build();

        app.UseOmiLocalization();

        if (!app.Environment.IsDevelopment())
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }
        //app.UseStatusCodePagesWithReExecute("/Error/{0}");
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseOmiLocalization();
        app.UseRouting();

        app
            .UseOmiConfigUI()
            .UseOmiLogUI()
            //.UseOmiSystemUI()
            //.UseOmiLoginUI()
            ;

        app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");

        // ��ʼ�����ݿ��
        await Task.Run(async () =>
        {
            //var _thisRepository = app.Services.GetRequiredService<ISqlSugarClient>();

            //if (!_thisRepository.DbMaintenance.IsAnyTable(nameof(AppDemo), false))
            //{
            //    _thisRepository.CodeFirst.InitTables<AppDemo>();
            //    _thisRepository.Insertable(new app_demo().HasData().ToList()).ExecuteCommand();
            //}

            // ��������в���
            if (args.Contains("--open"))
            {
                var httpUrl = builder.Configuration.GetValue<string>("Url");// ����Ĭ�ϼ�����ַ
                if (string.IsNullOrEmpty(httpUrl)) return;
                builder.WebHost.UseUrls(httpUrl);
                try { Process.Start(new ProcessStartInfo(httpUrl) { UseShellExecute = true }); }
                catch { using var client = new HttpClient(); await client.GetAsync(httpUrl); }
            }
        });

        app.Run();
    }
}